/*******************************************************************************
Jimm - Mobile Messaging - J2ME ICQ clone
Copyright (C) 2003-05  Jimm Project

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
********************************************************************************
File: src/jimm/DebugLog.java
Version: ###VERSION###  Date: ###DATE###
Author(s): Artyomov Denis
*******************************************************************************/

// #sijapp cond.if protocols_MRIM is "true" #
// #sijapp cond.if modules_MAGIC_EYE is "true" #
package protocol.mrim;

import DrawControls.icons.*;
import DrawControls.text.Parser;
import jimm.chat.message.Message;
import jimm.modules.*;
import jimm.ui.base.CanvasEx;
import jimm.ui.base.NativeCanvas;
import java.util.Vector;
import jimm.*;
import jimm.comm.*;
import jimm.ui.*;
import jimm.ui.menu.*;
import jimm.util.*;
import protocol.*;

public class MicroBlog extends TextListEx implements SelectListener, TextBoxListener {
    private Vector emails = new Vector();
    private Vector ids = new Vector();
    private Mrim mrim;
    private boolean hasNewMessage;
    
    public MicroBlog(Mrim mrim) {
        super(JLocale.getString("microblog"));
        this.mrim = mrim;
    }
    
    public void activate() {
        MenuModel menu = new MenuModel();
        menu.addItem("message",       MENU_WRITE);
        menu.addItem("user_menu",     MENU_USER_MENU);
        menu.addItem("copy_text",     MENU_COPY);
        menu.addItem("clear",         MENU_CLEAN);
        menu.setActionListener(this);
        menu.setDefaultItemCode(MENU_USER_MENU);
        setMenu(menu, MENU_WRITE);

        setCurrentItem(getSize());
        show();
    }
    protected void restoring() {
        hasNewMessage = false;
        super.restoring();
    }
    public Icon getIcon() {
        return hasNewMessage ? Message.msgIcons.iconAt(Message.ICON_IN_MSG_HI) : null;
    }
    
    private void removeOldRecords() {
        final int maxRecordCount = 50;
        while (maxRecordCount < emails.size()) {
            ids.removeElementAt(0);
            emails.removeElementAt(0);
            removeFirstText();
        }
    }

    public boolean addPost(String from, String nick, String post, String postid, boolean reply, long time) {
        if (StringConvertor.isEmpty(post) || ids.contains(postid)) {
            return false;
        }

        int counter = uiBigTextIndex++;
        String date = Util.getDateString(time, false);
        Contact contact = mrim.getItemByUIN(from);
        emails.addElement(from);
        ids.addElement(postid);

        Parser par = createParser(counter);
        par.useMinHeight();
        // #sijapp cond.if modules_DEBUGLOG is "true" #
        DebugLog.println("blog " + from + " (" + nick + ")");
        // #sijapp cond.end#
        if (null != contact) {
            nick = contact.getName();
        }
        if (StringConvertor.isEmpty(nick)) {
            nick = from;
        }
        par.addText(nick, THEME_MAGIC_EYE_USER, FONT_STYLE_PLAIN);
        if (reply) {
            par.addText(" (reply)", THEME_MAGIC_EYE_USER, FONT_STYLE_PLAIN);
        }
        par.addText(" " + date + ":", THEME_MAGIC_EYE_NUMBER, FONT_STYLE_PLAIN);
        par.doCRLF();
        //THEME_MAGIC_EYE_ACTION
        par.addTextWithSmiles(post, THEME_MAGIC_EYE_TEXT, FONT_STYLE_PLAIN);
        
        lock();
        addPar(par);
        removeOldRecords();
        unlock();
        if (this != Jimm.getJimm().getDisplay().getCurrentDisplay()) {
            hasNewMessage = true;
        }
        return true;
    }

    private static final int MENU_WRITE     = 0;
    private static final int MENU_COPY      = 1;
    private static final int MENU_CLEAN     = 2;
    private static final int MENU_USER_MENU = 3;
    
    private InputTextBox postEditor;
    private String replayTo = "";
    protected final void itemSelected() {
        String to = "";
        int cur = getCurrItem();
        if (cur < ids.size()) {
            to = (String)ids.elementAt(cur);
        }
        write(to);
    }
    private void write(String to) {
        replayTo = to;
        postEditor = new InputTextBox().create(null == to ? "message" : "reply", 250);
        postEditor.setTextBoxListener(this);
        postEditor.show();
    }
    public void select(Select select, MenuModel model, int cmd) {
        switch (cmd) {
            case MENU_WRITE:
                write("");
                return;
                
            case MENU_COPY:
                copy(false);
                break;

            case MENU_CLEAN:
                synchronized (this) {
                    emails.removeAllElements();
                    ids.removeAllElements();
                    clear();
                }
                break;

            case MENU_USER_MENU:
                try {
                    int item = getCurrTextIndex();
                    String uin = (String)emails.elementAt(item);
                    showMenu(mrim.createTempContact(uin).getContextMenu(mrim));
                } catch (Exception e) {
                }
                return;

            case MenuModel.BACK_COMMAND_CODE:
                back();
                return;
        }
        select.back();
    }
    protected final void doKeyReaction(int keyCode, int actionCode, int type) {
        if (CanvasEx.KEY_PRESSED == type) {
            switch (actionCode) {
                case NativeCanvas.NAVIKEY_FIRE:
                    if ('5' == keyCode) {
                        itemSelected();
                        return;
                    }
                    write(null);
                    return;
            }
        }
        super.doKeyReaction(keyCode, actionCode, type);
    }

    public void textboxAction(InputTextBox box, boolean ok) {
        if (ok && mrim.isConnected()) {
            MrimConnection c = mrim.getConnection();
            String text = postEditor.getString();
            if ((null != c) && !StringConvertor.isEmpty(text)) {
                c.postToMicroBlog(text, replayTo);
                setCurrentItem(getSize());
                restore();
            }
        }
    }
}
// #sijapp cond.end#
// #sijapp cond.end#
